package com.ljtxxhwj.myblob.controller;


import com.github.pagehelper.PageInfo;
import com.ljtxxhwj.myblob.entity.*;
import com.ljtxxhwj.myblob.service.ArticleKindService;

import com.github.pagehelper.PageInfo;
import com.ljtxxhwj.myblob.entity.Article;
import com.ljtxxhwj.myblob.entity.ArticleKind;
import com.ljtxxhwj.myblob.entity.ArticleWithTypeAndUname;
import com.ljtxxhwj.myblob.entity.User;
import com.ljtxxhwj.myblob.service.ArticleKindService;
import com.ljtxxhwj.myblob.entity.ArticleTag;
import com.ljtxxhwj.myblob.entity.Comment;
import com.ljtxxhwj.myblob.service.ArticleService;
import com.ljtxxhwj.myblob.service.DraftService;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljtxxhwj
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    private String pathString = "/home/ljt/Desktop/md/";

    private String pathString2 = "/home/ljt/Desktop/md/draft/";

    @Autowired
    private ArticleService articleService;

    @Autowired
    DraftService draftService;

    @GetMapping("/get.do")
    public List<Article> getAll() {
        List<Article> all = articleService.getAll();
//        all.forEach(System.out::println);
        return all;
    }
    @GetMapping("/getTotalSeeByUserId.do")
    public int getTotalSeeByUserId( int userId) {
        return articleService.getTotalSeeByUserId(userId);
    }
    @GetMapping("/getOne.do")
    public Article getOne(@Param("articleId") int articleId) {
        System.out.println("articleId = " + articleId);
        Article article = articleService.getOne(articleId);
        System.out.println("article = " + article);
        return article;
    }
    @GetMapping("/getArticlesByUserId.do")
    public List<Article> getArticlesByUserId( Integer userId) {
        return articleService.getArticlesByUserId(userId);
    }
    @PostMapping("/add.do")
    public String add(@RequestBody Article article) {
        System.out.println("article = " + article);
        Timestamp articleDate = new Timestamp(new java.util.Date().getTime());
//        System.out.println("article = " + article);
//        System.out.println("articleDate = " + articleDate);
//        String articleContent = article.getArticleContent();
//        System.out.println("articleContent = " + articleContent);

        Integer userId = article.getUser().getUserId();
        String filePath = userId + "_" + articleDate.getTime() + ".md";

        try (OutputStream output = new FileOutputStream(pathString+filePath)) {
            IOUtils.write(article.getArticleContent(), output);
        } catch (IOException e) {
            e.printStackTrace();
        }

        article.setArticleContent(filePath);
        article.setArticleDate(articleDate);
        articleService.add(article);
        return "{'add':'success'}";
    }

    @PostMapping("/modify.do")
    public String modify(@RequestBody Article article) {
//        System.out.println("article = " + article);
        articleService.modify(article);
        return "{'modify':'success'}";
    }



    @GetMapping("/delete.do")
    public String delete(@RequestParam("id") int id) {
//        System.out.println("id = " + id);
        Article one = articleService.getOne(id);
        articleService.deleteOne(id);
        String articleContent = one.getArticleContent();
        File file = new File(pathString + articleContent);
        file.delete();
        return "{'delete':'success'}";
    }

    @GetMapping("/readOne.do")
    public Article readOne(@RequestParam("articleId") int articleId) throws IOException {
        System.out.println("articleId = " + articleId);
        Article article = articleService.readOne(articleId);

        File file = new File(pathString +article.getArticleContent());
        String articleContent = IOUtils.toString(new FileInputStream(file), "utf8");
        System.out.println("articleContent = " + articleContent);

        article.setArticleContent(articleContent);
        System.out.println("article = " + article);
        return article;
    }

    @GetMapping("/readDraft.do")
    public Draft readDraft(@RequestParam("draftId") int draftId) throws IOException {
        System.out.println("draftId = " + draftId);
        Draft draft = draftService.get(draftId);

        File file = new File(pathString2 +draft.getDraftContent());
        String draftContent = IOUtils.toString(new FileInputStream(file), "utf8");

        draft.setDraftContent(draftContent);


        return draft;
    }

    @GetMapping("/getTag.do")
    public List<ArticleTag> getTag(@RequestParam("articleId") int articleId) {
        System.out.println("articleId = " + articleId);
        List<ArticleTag> articleTags = articleService.getTag(articleId);
//        System.out.println("articleTags = " + articleTags);
        return articleTags;
    }

    @GetMapping("/getComment.do")
    public List<Comment> getComment(@RequestParam("articleId") int articleId) {
        System.out.println("articleId = " + articleId);
        List<Comment> comment = articleService.getComment(articleId);
        System.out.println("comment = " + comment);
        return comment;
    }

    @PostMapping("/addComment.do")
    public String addComment(@RequestBody Comment comment) {
        comment.setCommentDate(new Date(new java.util.Date().getTime()));
//        System.out.println("articleId = " + articleId);
        System.out.println("comment = " + comment);
        articleService.addComment(comment);
        return "{'addComment':'success'}";
    }

    @GetMapping("/deleteComment.do")
    public String deleteComment(@RequestParam("commentId") int commentId) {
        System.out.println("commentId = " + commentId);
        articleService.deleteComment(commentId);
        return "{'deleteComment':'success'}";
    }

    @GetMapping("/getKind.do")
    public List<ArticleKind> getKind() {
        return articleService.getKind();
    }


    @RequestMapping("/deleteByArticleId.do")
    @ResponseBody
    public DialogResult deleteByArticleTagId(Integer articleId){
        DialogResult result = new DialogResult();
        try{
            articleService.deleteByArticleId(articleId);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setMsg("删除失败");
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/deleteBatch.do")
    public DialogResult deleteBatch(String articleIds, HttpServletRequest request){
        DialogResult result = new DialogResult();
        String[] ids = articleIds.split(",");
        for(String id : ids){
            int num = Integer.valueOf(id);
            articleService.deleteByArticleId(num);
        }

        try {

            result.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();

        }
        return result;
    }

    @RequestMapping("/loadWithPage.do")
    @ResponseBody
    public PageInfo<ArticleWithTypeAndUname> loadWithPage(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage, @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        PageInfo<ArticleWithTypeAndUname> pageInfo = articleService.loadWithPage(currentPage,pageSize);
        return pageInfo;
    }

    @RequestMapping("/findAllArticle.do")
    @ResponseBody
    public List<Article> findAllArticle(){
        return articleService.findAllArticle();
    }

    @RequestMapping("/findByTitle.do")
    @ResponseBody
    public List<Article> findByTitle(String articleTitle){
        return articleService.findByTitle(articleTitle);
    }

    @PostMapping("/getWant.do")
    public List<Article> getWant(@RequestBody Article article) {
        System.out.println("article = " + article);
        return articleService.getWant(article);
    }

    @PostMapping("/getWantAndNew.do")
    public List<Article> getWantAndNew(@RequestBody Article article) {
        System.out.println("article = " + article);
        return articleService.getWantAndNew(article);
    }


}
